<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Adminlist extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        // if (!cache('list')) {
        $list = Db::name('role_user')
            ->alias('lu')
            ->join('role r', 'lu.role_id = r.id')
            ->join('user u', 'lu.user_id = u.id')
            ->order('u.id asc')
            ->select();

        $count = Db::name('user')->count('id');

        // $list =
        // var_dump($list);die;
        // cache('list',$list);

        return view('adminlist/index', ['list' => $list,'count' => $count]);
        // }else{
        //     // $list = cache('list');
        // return view('adminlist/index',['list'=>$list]);
        // }
    }

    public function adminList()
    {
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
//        $data =  Db::name('role')->field('title')->select();
//          $data =  Db::name('role')->select();
//        var_dump($data);die;
        // $data =  Db::name('role')->select();
//        $res =  Db::name('role')->column('title');
        $res = Db::name('role')->distinct(true)->field('title')->select();
//        var_dump($res);die;

        return view('adminlist/create', ['data' => $res]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {

        // $list['logintime'] = date("Y-m-d H:i:s",time());
        // $d =  Db::name('role')->select();
        // echo '11';die;
        $p = $request->post();
//        dump($p);
//        die;

        $p['logintime'] = date("Y-m-d H:i:s", time());
        unset($p['repassword']);
        $data = [
            'name' => $p['name'],
            'password' => md5($p['password']),
            'email' => $p['email'],
            'phone' => $p['phone'],
            'logintime' => $p['logintime']
            // 'status' => $p['status']
        ];
        $res = Db::name("user")->insertGetId($data);
        // echo $res;die;
        if ($res > 0) {
//             $l = $request->post();
            $list = [
//                'rname' => $p['name'],
                'remark' => $p['remark'],
                'title' => $p['title'],
            ];
            $result = Db::name("role")->insertGetId($list);

            $re = [
                'user_id' => $res,
                'role_id' => $result
            ];
            $da = Db::name("role_user")->data($re)->insert();
            // echo $da;die;
            if ($result && $da) {
                return $this->success('添加成功', url('admin/Adminlist/index'));
            } else {
                return $this->error('添加失败');
            }

        } else {
            return $this->error('添加失败');
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // echo $id;die;
        $data = Db::name('role_user')
            ->alias('lu')
            ->join('role r', 'lu.role_id = r.id')
            ->join('user u', 'lu.user_id = u.id')
            ->where('u.id', '=', $id)
            ->order('u.id asc')->find();
        $res = Db::name('role')->distinct(true)->field('title')->select();
//         var_dump($res);die;
//         var_dump($data);die;
        return view('adminlist/edit', ['data' => $data, 'res' => $res]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // echo $id;die;
        $info = $request->put();
//         dump($info);die;

        $data = [
            'name' => $info['name'],
            'phone' => $info['phone'],
            'email' => $info['email'],
            // 'tilte'=> $info['title'],
            // 'remark'=> $info['remark'],
        ];
        // var_dump($data);die;
        $result = Db::name("user")->where('id', $id)->update($data);
//        dump($result);
//        die;
        // $res = Db::name('');


        if ($result) {
            return $this->success('编辑成功', url('admin/Adminlist/index'));
        } else {
            return $this->error('编辑失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        // echo  '111';die; 
        $result = Db::name('user')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的信息删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的信息删除失败!';
        }
        return json($info);

    }
}
